/**
 * Copyright (C) 2009-2016 All Rights Reserved.
 */
package com.runmi.webchat.aftersale.dbcore.config;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * @author luojianhui
 * @version $Id DataSourceConfig.java, v 0.1 2016-10-26 15:22 luojianhui Exp $$
 */
@Configuration
public class DataBaseConfiguration  {

    protected static final Log log = LogFactory.getLog(DataBaseConfiguration.class.getSimpleName());
    @Autowired
    public JdbcProperties jdbcProperties;

    @Bean(name="dataSource")
    @Primary
    public DataSource dataSource() {
        log.info("DataSource [" + jdbcProperties.toString() + "]");
        DruidDataSource datasource = new DruidDataSource();
        datasource.setDriverClassName(jdbcProperties.getDriverClassName());
        datasource.setUrl(jdbcProperties.getUrl());
        datasource.setUsername(jdbcProperties.getUsername());
        datasource.setPassword(jdbcProperties.getPassword());
        datasource.setInitialSize(Integer.valueOf(jdbcProperties.getInitialSize()));
        datasource.setMaxActive(Integer.valueOf(jdbcProperties.getMaxActive()));
        datasource.setMaxWait(Long.valueOf(jdbcProperties.getMaxWait()));
        datasource.setValidationQuery(jdbcProperties.getValidationQuery());

        return datasource;
    }

}
        